package com.hmc.modules.sjfx.util;

import com.hmc.modules.material.service.tqmtqp.Tqmtqp1Service;
import com.hmc.modules.sjfx.dto.sjfx.SaleOrderSupplierDTO;
import com.hmc.modules.sjfx.dto.sjfx.SphAndWglhDTO;
import com.hmc.modules.sjfx.service.tmm0001.Tmm0001Service;
import com.hmc.modules.sjfx.service.tpmmmba.TpmmmbaService;
import com.hmc.modules.sjfx.service.tpmmmda.TpmmmdaService;
import com.hmc.modules.sjfx.service.tpmmmea.TpmmmeaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 抗拉强度工具类
 */
@RestController
public class KlqdUtil {
    @Autowired
    private TpmmmbaService tpmmmbaService;

    @Autowired
    private TpmmmdaService tpmmmdaService;

    @Autowired
    private TpmmmeaService tpmmmeaService;

    @Autowired
    private Tmm0001Service tmm0001Service;
    /**
     * 整合炉号和原材料的试批号的关系
     * @return
     */
    public HashMap<String, String> getWglhAndSph(){
        HashMap<String, String> wglhAndSphMap = new HashMap<>();

        // 缓存直缝炉号和原材料的试批号的关系
        List<SphAndWglhDTO> sphAndWglhBAS = tpmmmbaService.queryTpmmmbaBySphAndWglh();

        for (SphAndWglhDTO sphAndWglhDTO:sphAndWglhBAS) {
            wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            if (!wglhAndSphMap.containsKey(sphAndWglhDTO.getOriginHeatNo())){
                wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            }
        }

        // 缓存螺旋炉号和原材料的试批号的关系
        List<SphAndWglhDTO> sphAndWglhDAS = tpmmmdaService.queryTpmmmdaBySphAndWglhDA();
        for (SphAndWglhDTO sphAndWglhDTO:sphAndWglhDAS) {
            wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            if (!wglhAndSphMap.containsKey(sphAndWglhDTO.getOriginHeatNo())){
                wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            }
        }

        // 缓存螺旋炉号和原材料的试批号的关系
        List<SphAndWglhDTO> sphAndWglhEAS = tpmmmeaService.queryTpmmmbaBySphAndWglhEA();
        for (SphAndWglhDTO sphAndWglhDTO:sphAndWglhEAS) {
            wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            if (!wglhAndSphMap.containsKey(sphAndWglhDTO.getOriginHeatNo())){
                wglhAndSphMap.put(sphAndWglhDTO.getOriginHeatNo(),sphAndWglhDTO.getSampleLotNo());
            }
        }

        return wglhAndSphMap;
    }

    /**
     * 管号和供应商的关联关系
     * @return
     */
    public HashMap<String, String> getGhAndSupplier() {
        // 创建一个用于存储管号和供应商关联关系的哈希映射
        HashMap<String, String> ghAndSupplierMap = new HashMap<>();

        // 查询销售订单子项和供应商的关联关系，并将其缓存到ghAndSupplierMap中
        // 获取销售订单子项和供应商的对应关系列表
        List<SaleOrderSupplierDTO> saleOrderSupplierDTOS = tmm0001Service.querySaleOrderSubAndSupplier();

        // 遍历对应关系列表
        for (SaleOrderSupplierDTO saleOrderSupplierDTO : saleOrderSupplierDTOS) {
            // 将销售订单子项和供应商的对应关系存储到ghAndSupplierMap中
            ghAndSupplierMap.put(saleOrderSupplierDTO.getSaleOrderSubNo(), saleOrderSupplierDTO.getSupplierName());

            // 如果ghAndSupplierMap中没有该销售订单子项的键，则再次将其销售订单子项和供应商的对应关系存储到ghAndSupplierMap中
            if (!ghAndSupplierMap.containsKey(saleOrderSupplierDTO.getSaleOrderSubNo())) {
                ghAndSupplierMap.put(saleOrderSupplierDTO.getSaleOrderSubNo(), saleOrderSupplierDTO.getSupplierName());
            }
        }

        // 返回存储管号和供应商关联关系的哈希映射ghAndSupplierMap
        return ghAndSupplierMap;
    }
}
